﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using tracker.Models;
using tracker.Models.BLL;
using tracker.Models.DAL;
using tracker.Models.DTO;

namespace tracker.Controllers
{
    public class HandlerAddOrRemoveDeviceController : Controller
    {
        //
        // GET: /HandlerAddOrRemoveDevice/
        [CallAjaxFilter]
        public JsonStringResult Index(
                string userid,
                string TrackerIconName, 
                string snimeiid, 
                string trackername, 
                string trackerpassword, 
                string trackersimnumber, 
                string trackertypeid,
                string type
            )
        {
            string result = "";
            int iflat=0;
            // Add new device
            if (type == "AddNew")
            {
                if (Utilities.regexCheck(@"^[\w\d]*$", userid)
                    && Utilities.regexCheck(@"^[0-9]{4,6}$", trackerpassword)
                    && Utilities.regexCheck(@"\w", trackername)
                    && Utilities.regexCheck(@"^[0-9]{5,16}$", trackersimnumber)
                    && Utilities.isInt(trackertypeid)
                    && !String.IsNullOrEmpty(userid)
                )
                {
                    // Check permission
                    String currentUserGroupID = Session[Common.USER_GROUPID].ToString();
                    bool validUser = false;
                    
                    // 2. Advance user, 3.admin user, 4.Group user
                    if (currentUserGroupID == "2")
                    {
                        // Check parent user of device is valid
                        validUser = (Session[Common.USER_SECUTIRY_CODE].ToString() == userid);
                       
                    }
                    else if (currentUserGroupID == "3" )
                    {                        
                        validUser = true;
                    }
                    else if (currentUserGroupID == "4")
                    {
                        // Check parent user of device is valid
                        validUser = (Session[Common.USER_SECUTIRY_CODE].ToString() == new EUserLoginBLL().getParentCode(userid));
                    }
                    else
                    {
                        //permission
                        validUser = false;
                    }

                    if (validUser)
                    {
                        // Check max device number
                        UserLoginDAL userDAL = new UserLoginDAL();
                        int numDevice = userDAL.getNumDeviceBySecurityCode(userid);
                        int sumDevice = userDAL.getSumDeviceBySecurityCode(userid);
                        if (numDevice > sumDevice)
                        {
                            //Insert
                            EDevice ent = new EDevice();
                            ent.CreateBy = Session[Common.USER].ToString();
                            ent.DeviceKey = snimeiid;
                            ent.DeviceName = trackername;
                            ent.DeviceTypeID = int.Parse(trackertypeid);
                            ent.Password = trackerpassword;
                            ent.PictureOfDevide = TrackerIconName;
                            ent.SimNumber = trackersimnumber;
                            EUserLogin parrentUser = new EUserLoginBLL().getBySecurityCode(userid);
                            if (parrentUser != null)
                            {
                                ent.UserID = parrentUser.UserID;
                                if (new DeviceDAL().insert(ent) == "0")
                                {
                                    iflat = 1;
                                }
                                else
                                {
                                    iflat = 0;
                                }
                            }
                            else
                            {
                                iflat = 0;
                            }
                        }
                        else
                        {
                            //Max device error
                            iflat = 4;
                        }
                        
                    }
                    else
                    {
                        //permission
                        iflat = 2;
                    }
                }
            }
            else if (type == "AddOld")
            {
                String tmp = new DeviceDAL().changeUser(userid, snimeiid, trackerpassword, trackersimnumber) ;
                if (tmp== "0")
                {
                    iflat = 1;
                }
                else
                {
                    iflat = 0;
                }
            }
            else if (type == "Remove")
            {
                string tmp = new DeviceDAL().removeDevice(userid, snimeiid, trackerpassword);
                if (tmp == "0")
                {
                    iflat = 1;
                }
                else
                {
                    iflat = 0;
                }
            }
            switch (iflat)
            {
                case 0: result = "{\"success\":\"iserror\"}";
                    break;
                case 1: result ="{\"success\":'true'}";
                    break;
                case 2: result ="{\"success\":\"nopermission\"}";
                    break;
                case 3: result ="{\"success\":\"exist\"}";
                    break;
                case 4: result ="{\"success\":\"maxlimit\"}";
                    break;
            }
            return new JsonStringResult(result);
        }

    }
}
